package com.sm.ui;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sm.domain.Student;
import com.sm.domain.Teacher;
import com.sm.service.IStudentService;
import com.sm.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录
 */
@Component
public class LoginUI extends JFrame {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TeacherMenuUI teacherMenuUI;
    @Autowired
    private StudentMenuUI studentMenuUI;
    // 主面板
    private JPanel p;
    // 标签
    private JLabel lblName, lblPwd;
    // 用户名，文本框
    private JTextField txtName;
    // 密码框
    private JPasswordField txtPwd;
    // 角色，单选按钮
    private JRadioButton rbAdmin, rbStudent;
    private JButton btnOk;


    // 构造方法
    public LoginUI(){
        super("登录");


        // 设置面板布局，网格布局
        p = new JPanel(new GridLayout(5, 1));
        JPanel decP = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel l = new JLabel("欢迎使用学生成绩发布系统！");
        l.setFont(new java.awt.Font("幼圆", 1, 24));
        decP.add(l);
        p.add(decP);
        // 实例化组件
        lblName = new JLabel("用户名：");
        lblPwd = new JLabel("密    码：");
        txtName = new JTextField(16);
        txtPwd = new JPasswordField(16);
        rbAdmin = new JRadioButton("老师");
        rbStudent = new JRadioButton("学生");
        rbAdmin.setSelected(true);
        ButtonGroup bg = new ButtonGroup();
        bg.add(rbAdmin);
        bg.add(rbStudent);
        btnOk = new JButton("登录");
        // 注册监听器，监听确定按钮
        btnOk.addActionListener(new LoginListener());
        // 将组件分组放入面板，然后将小面板放入主面板
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p1.add(lblName);
        p1.add(txtName);
        p.add(p1);
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p2.add(lblPwd);
        p2.add(txtPwd);
        p.add(p2);
        JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p3.add(rbAdmin);
        p3.add(rbStudent);
        p.add(p3);
        JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p8.add(btnOk);
        p.add(p8);
        // 主面板放入窗体中
        this.add(p);
        // 设置窗体大小和位置
        this.setSize(800, 600);
        // 设置窗口初始化居中
        this.setLocationRelativeTo(null);
        // 设置窗体不可改变大小
        this.setResizable(false);

        // 设置窗体初始可见
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void close(){
        this.dispose();
    }

    // 监听类，负责处理确认按钮的业务逻辑
    private class LoginListener implements ActionListener {
        // 重写actionPerFormed()方法，事件处理方法
        public void actionPerformed(ActionEvent e){
            // 获取用户输入的数据
            String userName = txtName.getText().trim();
            String password = new String(txtPwd.getPassword());
            if (rbAdmin.isSelected()) {
                QueryWrapper<Teacher> qw = new QueryWrapper<>();
                qw.eq("teaNo", userName).eq("password", password);
                Teacher tea = teacherService.getOne(qw);
                if (tea != null) {

                    redisTemplate.opsForValue().set("teacherId", tea.getId());
                    redisTemplate.opsForValue().set("teaNo", tea.getTeaNo());
                    toAdminView();
                }
                else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "错误提示", JOptionPane.ERROR_MESSAGE);

                }
            }
            if (rbStudent.isSelected()) {
                QueryWrapper<Student> qw = new QueryWrapper<>();
                qw.eq("stuNo", userName).eq("password", password);
                Student stud = studentService.getOne(qw);
                if (stud != null) {
                    redisTemplate.opsForValue().set("studentId", stud.getId());
                    redisTemplate.opsForValue().set("stuName", stud.getName());
                    toStudentView();
                }
                else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "错误提示", JOptionPane.ERROR_MESSAGE);
                }
            }
        }

        private void toAdminView(){
            close();
            teacherMenuUI.show();
            teacherMenuUI.init();
        }

        private void toStudentView(){
            close();
            studentMenuUI.show();
            studentMenuUI.init();
        }
    }

}


